package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.mj.dollars.AppConnect;
import com.mj.mafeed.R;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.MyGridView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

/**
 * 会员
 * @author zhaominglei
 * @date 2016-1-12
 * 
 */
public class MemberActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = MemberActivity.class.getSimpleName();
	private static final String MENU_TEXT_FAVORITE = "我的收藏";
	private static final String MENU_TEXT_BOOK = "我的预约";
	private static final String MENU_TEXT_RECEIVER = "收货地址";
	private static final String MENU_TEXT_FREIGHT = "物流费用";
	private static final String MENU_TEXT_SERVICES = "服务条款";
	private static final String MENU_TEXT_ABOUT = "关于App";
	private boolean isExit = false;
	private TimerTask timerTask;
	private TextView settingTxt; //设置
	private ImageView memberMessage;
	private Button registerLogin; //登录/注册
	private ImageView headpicView; //头像
	private TextView usernameTxt; //用户名
	private String username; //用户名
	private String name; //姓名
	private String sign; //签名
	private TextView orderTitleRightTxt; //查看全部订单
	private ImageView orderNotpay; //待支付
	private TextView orderNotpayTxt; //待支付
	private ImageView orderNotsend; //待发货
	private TextView orderNotsendTxt; //待发货
	private ImageView orderNotreceive; //待收货
	private TextView orderNotreceiveTxt; //待收货
	private MyGridView gridView; //添加图片视图
	private Bitmap bitmap; //导入临时图片
	private ArrayList<HashMap<String, Object>> imageItem = new ArrayList<HashMap<String,Object>>();
	private SimpleAdapter simpleAdapter;
	@SuppressWarnings("serial")
	private Map<Integer, String> menuMap = new LinkedHashMap<Integer, String>(){
		{
			put(R.drawable.member_favorite, MENU_TEXT_FAVORITE);
			put(R.drawable.member_book, MENU_TEXT_BOOK);
			put(R.drawable.member_receiver, MENU_TEXT_RECEIVER);
			put(R.drawable.member_freight, MENU_TEXT_FREIGHT);
			put(R.drawable.member_services, MENU_TEXT_SERVICES);
			put(R.drawable.member_about, MENU_TEXT_ABOUT);
		}
	};
	private Button logout; //安全退出
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.activity_member);
		init();
	}
	
	private void init() {
		settingTxt = (TextView)findViewById(R.id.member_setting);
		memberMessage = (ImageView)findViewById(R.id.member_message);
		registerLogin = (Button)findViewById(R.id.member_login);
		headpicView = (ImageView)findViewById(R.id.member_headpic);
		usernameTxt = (TextView)findViewById(R.id.member_username);
		orderTitleRightTxt = (TextView)findViewById(R.id.member_order_title_right);
		orderNotpay = (ImageView)findViewById(R.id.member_order_notpay);
		orderNotpayTxt = (TextView)findViewById(R.id.member_order_notpay_txt);
		orderNotsend = (ImageView)findViewById(R.id.member_order_notsend);
		orderNotsendTxt = (TextView)findViewById(R.id.member_order_notsend_txt);
		orderNotreceive = (ImageView)findViewById(R.id.member_order_notreceive);
		orderNotreceiveTxt = (TextView)findViewById(R.id.member_order_notreceive_txt);
		gridView = (MyGridView)findViewById(R.id.member_gridView);
		logout = (Button)findViewById(R.id.member_logout);
		
		settingTxt.setOnClickListener(this);
		memberMessage.setOnClickListener(this);
		registerLogin.setOnClickListener(this);
		orderTitleRightTxt.setOnClickListener(this);
		orderNotpay.setOnClickListener(this);
		orderNotpayTxt.setOnClickListener(this);
		orderNotsend.setOnClickListener(this);
		orderNotsendTxt.setOnClickListener(this);
		orderNotreceive.setOnClickListener(this);
		orderNotreceiveTxt.setOnClickListener(this);
		logout.setOnClickListener(this);
		//init member menu
		for (Map.Entry<Integer, String> entry : menuMap.entrySet()) {
			bitmap = BitmapFactory.decodeResource(getResources(), entry.getKey());
			HashMap<String,Object> map = new HashMap<String, Object>();
			map.put("itemImage", bitmap);
			map.put("itemText", entry.getValue());
			imageItem.add(map);
			simpleAdapter = new SimpleAdapter(this, imageItem, R.layout.griditem_membermenu, new String[]{ "itemImage", "itemText" }, new int[]{ R.id.griditem_membermenu_pic,R.id.griditem_membermenu_txt });
			simpleAdapter.setViewBinder(new ViewBinder() {
				@Override
				public boolean setViewValue(View view, Object data,
						String textRepresentation) {
					if (view instanceof ImageView && data instanceof Bitmap) {
						ImageView imageView = (ImageView)view;
						imageView.setImageBitmap((Bitmap)data);
						return true;
					}
					return false;
				}
			});
			gridView.setAdapter(simpleAdapter);
		}
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
				int i = 0;
				for (Map.Entry<Integer, String> entry : menuMap.entrySet()) {
					if (i == position) {
						String menuText = entry.getValue();
						if (menuText.equals(MENU_TEXT_FAVORITE)) {
							if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
								intent = new Intent(MemberActivity.this, FavoriteListActivity.class);
								intent.putExtra("referer", "MemberActivity");
								startActivity(intent);
							} else {
								Toast.makeText(MemberActivity.this, R.string.check_login_error, Toast.LENGTH_SHORT).show();
							}
						} else if (menuText.equals(MENU_TEXT_BOOK)) {
							if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
								intent = new Intent(MemberActivity.this, BookLogListActivity.class);
								intent.putExtra("referer", "MemberActivity");
								startActivity(intent);
							} else {
								Toast.makeText(MemberActivity.this, R.string.check_login_error, Toast.LENGTH_SHORT).show();
							}
						} else if (menuText.equals(MENU_TEXT_RECEIVER)) {
							if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
								intent = new Intent(MemberActivity.this, ReceiverListActivity.class);
								intent.putExtra("referer", "MemberActivity");
								startActivity(intent);
							} else {
								Toast.makeText(MemberActivity.this, R.string.check_login_error, Toast.LENGTH_SHORT).show();
							}
						} else if (menuText.equals(MENU_TEXT_FREIGHT)) {
							Toast.makeText(MemberActivity.this, R.string.wait_dev, Toast.LENGTH_SHORT).show();
						} else if (menuText.equals(MENU_TEXT_SERVICES)) {
							Toast.makeText(MemberActivity.this, R.string.wait_dev, Toast.LENGTH_SHORT).show();
						} else if (menuText.equals(MENU_TEXT_ABOUT)) {
							intent = new Intent(MemberActivity.this, AboutActivity.class);
							intent.putExtra("referer", "MemberActivity");
							startActivity(intent);
						}
					}
					i++;
				}
			}
		});
		
//		referer = getIntent().getStringExtra("referer");
//      if (referer != null && referer.equals("LoginActivity")) {
//        	int member_login_success = getIntent().getIntExtra("member_login_success", 0);
//        	if (member_login_success == 1) {
//        		
//			}
//        } else if (referer != null && referer.equals("RegisterActivity")) {
//        	int member_register_success = getIntent().getIntExtra("member_register_success", 0);
//        	if (member_register_success == 1) {
//        		
//			}
//		}
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		name = (String)SPUtils.get(getApplicationContext(), "name", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
			String showName = (name != null && !name.equals("")) ? name : username;
			usernameTxt.setText(showName+getResources().getString(R.string.member_welcome_msg));
			settingTxt.setVisibility(View.VISIBLE);
			usernameTxt.setVisibility(View.VISIBLE);
			headpicView.setVisibility(View.VISIBLE);
			registerLogin.setVisibility(View.GONE);
			logout.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.member_setting:
			if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
				intent = new Intent(MemberActivity.this, SettingActivity.class);
				intent.putExtra("referer", "MemberActivity");
				startActivity(intent);
				MemberActivity.this.finish();
			} else {
				Toast.makeText(MemberActivity.this, R.string.check_login_error, Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.member_message:
			
			break;
		case R.id.member_login:
			intent = new Intent(MemberActivity.this, LoginActivity.class);
			intent.putExtra("referer", "MemberActivity");
			startActivity(intent);
			MemberActivity.this.finish();
			break;
		case R.id.member_order_title_right:
			goOrderlist(v, null);
			break;
		case R.id.member_order_notpay:
			goOrderlist(v, "notpay");
			break;
		case R.id.member_order_notpay_txt:
			goOrderlist(v, "notpay");
			break;
		case R.id.member_order_notsend:
			goOrderlist(v, "notsend");
			break;
		case R.id.member_order_notsend_txt:
			goOrderlist(v, "notsend");
			break;
		case R.id.member_order_notreceive:
			goOrderlist(v, "notreceive");
			break;
		case R.id.member_order_notreceive_txt:
			goOrderlist(v, "notreceive");
			break;	
		case R.id.member_logout:
			SPUtils.clear(getApplicationContext());
			usernameTxt.setVisibility(View.GONE);
			headpicView.setVisibility(View.GONE);
			registerLogin.setVisibility(View.VISIBLE);
			logout.setVisibility(View.GONE);
			break;	
		default:
			break;
		}
	}

	private void goOrderlist(View v, String string) {
		if (username != null && !username.equals("") && sign != null && !sign.equals("")) {
			intent = new Intent(MemberActivity.this, OrderListActivity.class);
			intent.putExtra("referer", "MemberActivity");
			startActivity(intent);
		} else {
			Toast.makeText(MemberActivity.this, R.string.check_login_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onBackPressed() {
		if (isExit) {
			AppConnect.getInstance(this).close();
			MemberActivity.this.finish();
		} else {
			isExit = true;
			Toast.makeText(MemberActivity.this, R.string.exit_msg, Toast.LENGTH_SHORT).show();
			timerTask = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			new Timer().schedule(timerTask, 2*1000);
		}
	}
}
